package com.hanbang.maige.view.home.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.hanbang.maige.R;
import com.hanbang.maige.code.base.view.fragment.BaseSwipeFragment;
import com.hanbang.maige.mode.enumeration.BannerType;
import com.hanbang.maige.mode.httpresponse.HomeResponse;
import com.hanbang.maige.mode.javabean.base.BannerAdData;
import com.hanbang.maige.mode.javabean.home.NewsItemData;
import com.hanbang.maige.mode.javabean.huodong.HuoDongItemData;
import com.hanbang.maige.pressenter.home.HomePresenter;
import com.hanbang.maige.view.home.IHomeBaseView;
import com.hanbang.maige.view.home.activity.HomeActivity;
import com.hanbang.maige.view.home.activity.NewsListActivity;
import com.hanbang.maige.view.home.adapter.NewsAdapter;
import com.hanbang.maige.view.huodong.activity.HuoDongDetailActivity;
import com.hanbang.maige.view.huodong.adapter.HuoDongAdapter;
import com.hanbang.maige.view.other.TestActivity;
import com.hanbang.maige.view.other.WebViewActivity;
import com.hanbang.maige.widget.banner.ConvenientBanner;
import com.hanbang.maige.widget.banner.NetworkImageHolderView;
import com.hanbang.maige.widget.banner.holder.CBViewHolderCreator;
import com.hanbang.maige.widget.empty_layout.ContextData;
import com.hanbang.maige.widget.gridlayout.SuperGridLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者　　: 李坤
 * 创建时间:2016/10/17　15:45
 * 邮箱　　：496546144@qq.com
 * <p>
 * 功能介绍：
 */

public class HomeFragment extends BaseSwipeFragment<IHomeBaseView.IHomeView, HomePresenter>
        implements IHomeBaseView.IHomeView, CBViewHolderCreator<NetworkImageHolderView>,
        SuperGridLayout.OnItemClickListener<NewsItemData>, HuoDongAdapter.OnButtnClickListener {

    @BindView(R.id.banner)
    ConvenientBanner banner;
    @BindView(R.id.zhixunGrid)
    SuperGridLayout zhixunGrid;
    @BindView(R.id.huodongGrid)
    SuperGridLayout huodongGrid;
    @BindView(R.id.huodongLL)
    LinearLayout huodongLL;
    @BindView(R.id.zhixunLL)
    LinearLayout zhixunLL;

    HomeResponse homeResponse = HomeResponse.init();


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        presenter.getHttpData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public HomePresenter initPressenter() {
        return new HomePresenter();
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        super.initView();
        toolbar.setTitle("首页");
        toolbar.setStatusViewHeight();
        toolbar.addAction(0, "搜索", R.drawable.material_search);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(activity, TestActivity.class);
                startActivity(intent);
                return false;
            }
        });

        banner.setPageIndicator(new int[]{R.drawable.banner_circle_default, R.drawable.banner_circle_select});
        ((TextView) huodongLL.getChildAt(0)).setText("活动");
        ((TextView) zhixunLL.getChildAt(0)).setText("资讯");

        zhixunGrid.setAdapter(newsAdapter);
        huodongGrid.setAdapter(huoDongAdapter);
        zhixunGrid.setOnItemClickListener(this);
        huodongGrid.setOnItemClickListener(new SuperGridLayout.OnItemClickListener<HuoDongItemData>() {
            @Override
            public void onItemClick(GridLayout parent, View view, int position, HuoDongItemData data) {
                HuoDongDetailActivity.startUI(getContext(), data.getActivityID());
            }
        });
    }


    @Override
    public void updataUI(HomeResponse response) {
        homeResponse.addData(response);
        banner.setPages(this, homeResponse.getGuanggao());
        newsAdapter.notifyDataSetChanged();
        huoDongAdapter.notifyDataSetChanged();

    }

    @Override
    public void baomingSuccess(String msg, int postion) {
        MaterialDialog dialog = new MaterialDialog.Builder(getContext())
                .title("提示")
                .content(msg)
                .positiveText("知道了")
                .build();
        dialog.show();
    }

    @Override
    public void onBaoming(int position, String activityId) {
        presenter.postBaoming(activityId, position);
    }

    @Override
    public void clearData() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public NetworkImageHolderView createHolder() {
        return new NetworkImageHolderView() {
            @Override
            public void onItemClicklistener(View item, int position, BannerAdData data) {
                if (data.getOpentype() == BannerType.HUODONG) {
                    HuoDongDetailActivity.startUI(getContext(), data.getTiaozhuanValue());
                } else if (data.getOpentype() == BannerType.NEWS) {
                    WebViewActivity.startUI(getContext(), "新闻", data.getTiaozhuanValue());
                } else if (data.getOpentype() == BannerType.WAI_HTML) {
                    Uri uri = Uri.parse(data.getTiaozhuanValue());
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }
            }
        };
    }

    NewsAdapter newsAdapter = new NewsAdapter(getContext(), homeResponse.getNews());
    HuoDongAdapter huoDongAdapter = new HuoDongAdapter(getContext(), homeResponse.getHuodong(), this);

    @Override
    public void onRefresh() {
        presenter.getHttpData();
    }

    @Override
    public void onRetryClick(ContextData data) {
        presenter.getHttpData();
    }

    @Override
    public void onEmptyClick(ContextData data) {
        presenter.getHttpData();
    }

    @OnClick(value = {R.id.huodongLL, R.id.zhixunLL})
    protected void onClick(View v) {
        if (v.getId() == R.id.huodongLL) {
            ((HomeActivity) activity).setCurrentItem(1);
        } else if (v.getId() == R.id.zhixunLL) {
            NewsListActivity.startUI(getContext());
        }
    }


    @Override
    public void onItemClick(GridLayout parent, View view, int position, NewsItemData data) {
        if (parent == zhixunGrid) {
            WebViewActivity.startUI(getContext(), data.getTitle(), data.getUrl());
        }

    }

}
